package com.mljr.example.dao;

import android.content.Context;

import com.ctakit.sdk.SdkApplication;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.table.TableUtils;
import com.mljr.example.db.DBHelper;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

/**
 * Helper to the database, manages versions and creation
 */
public abstract class BaseDAO<T> {

    private static DBHelper mDBHelper;
    protected Dao<T, Serializable> dao;

    public Dao<T, Serializable> getDao() {

        return initDao();
    }

    private Dao<T, Serializable> initDao() {


        try {
            if (dao == null) {
                if (mDBHelper == null) {
                    mDBHelper = getInstance(SdkApplication.app());
                }
                dao = mDBHelper.getDao(getClazz());
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return dao;
    }

    public BaseDAO(Context context) {
        initDao();
    }

    public static DBHelper getInstance(Context context) {
        if (mDBHelper == null) {
            mDBHelper = new DBHelper(context);
        }
        return mDBHelper;
    }

    public static void exit() {
        mDBHelper = null;
    }

    public void save(T t) {

        try {
            getDao().createOrUpdate(t);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public List<T> getAll() {
        try {
            return getDao().queryForAll();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public void deleteAll() {
        try {
            TableUtils.clearTable(getDao().getConnectionSource(), getClazz());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void deleteByProperties(String columnName, Serializable value) {
        try {
            DeleteBuilder<T, Serializable> del = getDao().deleteBuilder();
            del.where().eq(columnName, value);
            del.delete();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void deleteByExtProperties(String columnName, Serializable value) {
        try {
            DeleteBuilder<T, Serializable> del = getDao().deleteBuilder();
            del.where().ne(columnName, value);
            del.delete();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public T getById(Serializable id) {
        try {
            return getDao().queryForId(id);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    abstract Class<T> getClazz();

}
